const domainName = getApp().globalData.domainName
const header = {'content-type':'application/x-www-form-urlencoded'}
const dataType = 'json'
const method = 'POST'
const responseType = 'text'

const user_id = wx.getStorageSync('user_id')

// pages/update_service_tel/update_service_tel.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    service_tel_list:[],
  },

  // 删除电话
  selete_phone(e){
    let index = e.currentTarget.id
    let service_tel_list = this.data.service_tel_list
    service_tel_list.splice(index,1)
    this.setData({
      service_tel_list:service_tel_list
    })

  },

  service_tel(e){
    let phone = e.detail.value
    let index = e.currentTarget.id
    let service_tel_list = this.data.service_tel_list
    service_tel_list[index] = phone
    this.setData({
      service_tel_list:service_tel_list,
    })
  },

  add_service_tel(){
    let service_tel_list = this.data.service_tel_list
    service_tel_list.push(null)
    this.setData({
      service_tel_list:service_tel_list
    })
  },

  submit(){
    if(this.data.service_tel_list[0].length<10){
      wx.showToast({
        title: '请正确填写',
        duration: 1500,
        icon: 'error',
      })
      return
    }
    wx.showModal({
      content: '确定提交吗？',
      title: '提示',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/update_service_tel',
            data: {
              user_id:user_id,
              jg_id:this.data.jg_id,
              service_tel_list:JSON.stringify(this.data.service_tel_list || []),
            },
            dataType: dataType,
            enableCache: true,
            enableHttp2: true,
            enableQuic: true,
            header: header,
            method: method,
            responseType: responseType,
            timeout: 0,
            success: res => {
              if(res.data.update_res=='ok'){
                wx.showModal({
                  content: '提交完成',
                  showCancel: false,
                  title: '提示',
                  success: res => {
                    wx.navigateBack()
                  },
                })
              }
            },
          })
        }
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options',options)
    let jg_id = options.jg_id
    this.setData({
      service_tel_list:JSON.parse(decodeURIComponent(options.service_tel_list)),
      jg_id:jg_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})